package com.mine.springmvc.syn;

// 死锁：多个线程互相抱着对方的的资源，然后形成僵持
public class DeadLock {

}


//口红

class Lipstick{

}

//镜子

class Mirror{

}

class Makeup extends Thread{

    static Lipstick lipstick = new Lipstick();

    static Mirror mirror = new Mirror();

    int choice;

    String girlName;

    Makeup( int choice,String girlName){
        this.choice = choice;
        this.girlName = girlName;
    }

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    private void makeup() throws InterruptedException {
        if(choice == 0){
            synchronized (lipstick){
                System.out.println(this.girlName+"获得口红所");
                Thread.sleep(2000);
                synchronized (mirror){
                    System.out.println(this.girlName+"获得镜子锁");


                }
            }

        }else{

            synchronized (mirror){
                System.out.println(this.girlName+"获得镜子锁");
                Thread.sleep(2000);
                synchronized (lipstick){
                    System.out.println(this.girlName+"获得口红锁");


                }
            }

        }
    }


}
